package com.neu.service;

import com.neu.dao.ShoppingSystemMapper;
import com.neu.vo.ShoppingSystemVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

/**
 * 配置管理
 */
@Service
public class ShoppingSystemService {
    @Autowired
   private ShoppingSystemMapper shoppingSystemMapper;

    /**
     * 获取商城首页配置信息数据
     * @return
     */
     public Map<String,Object> listWx(){
         List<ShoppingSystemVo> list=shoppingSystemMapper.selectByExampleWx();
         System.out.println(list);
         Map<String,Object> map=new HashMap<>();
         for (ShoppingSystemVo item: list){
             map.put(item.getKey_name(),item.getKey_value());
         }
         return map;
  }

    /**
     * 获取商场配置信息数据
     * @return
     */
    public Map<String,Object> listMail(){
        List<ShoppingSystemVo> list=shoppingSystemMapper.selectByExampleMall();
        System.out.println(list);
        Map<String,Object> map=new HashMap<>();
        for (ShoppingSystemVo item: list){
            map.put(item.getKey_name(),item.getKey_value());
        }
        return map;
    }

    /**
     * 修改商城首页配置
     * @param map
     * @return
     */
    public int updateConfigWx(Map<String,String> map){
        try {
            Iterator iter = map.entrySet().iterator();
            while (iter.hasNext()) {
                Map.Entry entry = (Map.Entry) iter.next();
                String key = (String) entry.getKey();
                String val = (String) entry.getValue();
                shoppingSystemMapper.updateByExampleSelectiveWx(key,val);
            }
            return 1;
        }catch (Exception e){
            e.printStackTrace();
            return 0;
        }
    }
    public int updateConfigMall(Map<String,String> map){
        try {
            Iterator iter = map.entrySet().iterator();
            while (iter.hasNext()) {
                Map.Entry entry = (Map.Entry) iter.next();
                String key = (String) entry.getKey();
                String val = (String) entry.getValue();
                shoppingSystemMapper.updateByExampleSelectiveMall(key,val);
            }
            return 1;
        }catch (Exception e){
            e.printStackTrace();
            return 0;
        }
    }






//    @Autowired
//    private ShoppingSystemMapper systemMapper;
//    public Map<String,String> listWx(){
//        Map map = new HashMap();
//        map.put("list",systemMapper.selectByExample());
//        return map;
//    }

    public Map<String, Object> listExpress() {
        List<ShoppingSystemVo> list=shoppingSystemMapper.selectByExample();
        System.out.println(list);
        Map<String,Object> map=new HashMap<>();
        for (ShoppingSystemVo item: list){
            map.put(item.getKey_name(),item.getKey_value());
        }
        return map;
    }

    //    public int updateConfig(Map<String, String> data){
//        return shoppingSystemMapper.updateByExampleSelective();
//    }
    public int updateConfig(Map<String,String> map){
        try {
            Iterator iter = map.entrySet().iterator();
            while (iter.hasNext()) {
                Map.Entry entry = (Map.Entry) iter.next();
                String key = (String) entry.getKey();
                String val = (String) entry.getValue();
                shoppingSystemMapper.updateByExampleSelective(key,val);
            }
            return 1;
        }catch (Exception e){
            e.printStackTrace();
            return 0;
        }
    }
}
